package util;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

import javax.sql.DataSource;

import com.alibaba.druid.pool.DruidDataSource;

public class DBPoolUtils {
	private static DataSource dataSource;
	
	static {
		try {
			Properties pro = new Properties();
			pro.load(new FileInputStream("./conf/jdbc.properties"));
			String driver = pro.getProperty("jdbc.driver");
			String url = pro.getProperty("jdbc.url");;
			String username = pro.getProperty("jdbc.username");;
			String password = pro.getProperty("jdbc.password");;
			
			// 数据库连接池
			DruidDataSource ds = new DruidDataSource();
			ds.setDriverClassName(driver);
			ds.setUrl(url);
			ds.setUsername(username);
			ds.setPassword(password);
			ds.setMinIdle(10);
			ds.setInitialSize(10);
			ds.setMaxActive(30);
			ds.setMaxWait(3600);
			
			dataSource = ds;
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static Connection getConnection() throws SQLException {
		return dataSource.getConnection();
	}
}
